﻿using System;
using System.Data.Entity;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
using Sharp.Domain.Repository.EF;
using Sharp.Domain.WorkflowDemoBoundedContext.Migrations;

namespace Sharp.Domain.WorkflowDemoBoundedContext
{
    /// <inheritdoc />
    /// <summary>
    ///     平台数据访问上下文。
    /// </summary>
    //[DbConfigurationType(typeof(MyConfiguration))]
#if MYSQL
    [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
#endif
    public partial class Entities : SharpEntities
    {
        private readonly PluralizationService _service = PluralizationService.CreateService(new CultureInfo("en-US"));

        #region 构造函数

        static Entities()
        {

#if DEBUG
            Database.SetInitializer<Entities>(new MigrateDatabaseToLatestVersion<Entities, Configuration>());
#else
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<Entities, Configuration>());
#endif

        }

        /// <inheritdoc />
        /// <summary>
        ///     构造函数
        /// </summary>
        public Entities() : base("Entities")
        {
            Configuration.AutoDetectChangesEnabled = true;
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
            Configuration.UseDatabaseNullSemantics = true;
        }

        #endregion


        #region OnModelCreating

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Types().Configure(entity =>
            {
                entity.ToTable("Demo_" + _service.Pluralize(entity.ClrType.Name));

            });

            modelBuilder.Configurations.AddFromAssembly(typeof(Entities).Assembly);

        }

        #endregion
    }
}